package com.laxcen.basiclib.base;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.laxcen.basiclib.R;
import com.laxcen.basiclib.utils.DisplayUtil;
import com.laxcen.basiclib.utils.StringUtil;


/**
 * Create by 游德禄 on 2019-9-21
 *
 * 基础对话框
 *
 */
public abstract class BaseDialog extends Dialog {
    public boolean marchParent;
    public Context context;
    
    private ViewFlipper v;
    private ScrollView textBox;
    private TextView title,text,submitTxt,cancelTxt;
    private ImageView close;
    private View titleLine;
    private LinearLayout box,body,submit,cancel,head;
    public boolean isShow = false ;
    public View view ;
    private String id;
    private String tempData;
    private Object tempObject;

    public Object getTempObject() {
        return tempObject;
    }

    public void setTempObject(Object tempObject) {
        this.tempObject = tempObject;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTempData() {
        return tempData;
    }

    public void setTempData(String tempData) {
        this.tempData = tempData;
    }

    public BaseDialog(Context context) {
        this(context,-1);
    }
    public BaseDialog(Context context, int r) {
        this(context,r,true);
    }
    public BaseDialog(Context context, int r, boolean marchParent) {
        super(context);
        this.context = context;
        this.marchParent = marchParent;
        this.getContext().setTheme(android.R.style.Theme_InputMethod);
        setContentView(R.layout.dialog_base);
        view = super.getWindow().getDecorView();
        Window window = getWindow();
        WindowManager.LayoutParams attributesParams = window.getAttributes();
        attributesParams.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
        attributesParams.dimAmount = 0.5f;
        window.setAttributes(attributesParams);
        window.setLayout(marchParent?LinearLayout.LayoutParams.MATCH_PARENT:LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        setOnDismissListener(dialogInterface -> {
            if(onDismiss!=null){
                onDismiss.dismiss();
            }
        });
        setOutsideTouchable(true);
        initView(r);
    }

    private void initView(int r){
        v = view.findViewById(R.id.content);
        if(r!=-1) {
            View child = LayoutInflater.from(context).inflate(r, null);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            v.addView(child, lp);
        }else{
            v.setVisibility(View.GONE);
        }
        title = view.findViewById(R.id.title);
        titleLine = view.findViewById(R.id.titleLine);
        text = view.findViewById(R.id.text);
        textBox = view.findViewById(R.id.textBox);
        close = view.findViewById(R.id.close);
        submit = view.findViewById(R.id.submit);
        cancel = view.findViewById(R.id.cancel);
        box = view.findViewById(R.id.box);
        body = view.findViewById(R.id.body);
        head = view.findViewById(R.id.head);
        submitTxt=view.findViewById(R.id.submitTxt);
        cancelTxt=view.findViewById(R.id.cancelTxt);
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
                onClose();
            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onCancel()) {
                    if(onResult!=null){
                        onResult.onNo();
                    }
                    dismiss();
                }
            }
        });
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onSubmit()) {
                    if(onResult!=null){
                        onResult.onYes();
                    }
                    dismiss();
                }
            }
        });
        setOnDissmissListener(new OnDismiss() {
            @Override
            public void dismiss() {
                isShow = false;
                onClose();
            }
        });
    }

    public void setOutsideTouchable(boolean touchable){
        setCanceledOnTouchOutside(touchable);
    }
    public void setTouchable(boolean touchable){
        setCancelable(touchable);
    }
    public void setFocusable(boolean focusable){

    }
    public View getView(){
        return view;
    }
    public BaseDialog setOnDissmissListener(OnDismiss onDismiss){
        this.onDismiss = onDismiss;
        return this;
    }
    /**
     * 关闭
     */
    private OnDismiss onDismiss;
    public interface OnDismiss{
        void dismiss();
    }

    private int position;
    /**
     * 获得打开时的位置
     * @return
     */
    public int getPosition(){
        return position;
    }


    /**
     * 设置两边离屏幕边缘的距离
     * @param pd
     */
    public BaseDialog setPaddingLR(int pd){
        if(pd>0){
             box.setPadding(pd,0,pd,0);
        }
        return this;
    }

    /**
     * 设置背景
     * @param R
     */
    public BaseDialog setBg(int R){
        body.setBackgroundResource(R);
        return this;
    }

    /**
     * 设置标题
     * @param title
     */
    public BaseDialog setTitle(String title){
        if(StringUtil.isEmpty(title)){
            titleLine.setVisibility(View.GONE);
        }else{
            titleLine.setVisibility(View.VISIBLE);
        }
        this.title.setText(title);
        return this;
    }

    public BaseDialog setSubmitTxt(String txt){
        this.submitTxt.setText(txt);
        return this;
    }

    public BaseDialog setCancelTxt(String txt){
        this.cancelTxt.setText(txt);
        return this;
    }

    /**
     * 设置内容
     * @param text
     */
    public BaseDialog setContent(String text){
        if(this.text.getVisibility()== View.GONE) {
            this.textBox.setVisibility(View.VISIBLE);
            this.text.setVisibility(View.VISIBLE);
        }
        this.text.setText(text);
        return this;
    }

    public void setContentGravity(int gravity){
        text.setGravity(gravity);
    }
    public String getText(){
        return text.getText().toString();
    }

    /**
     * 隐藏标题
     */
    public BaseDialog hideHeader(){
        this.head.setVisibility(View.GONE);
        this.titleLine.setVisibility(View.GONE);
        return this;
    }

    public BaseDialog showHeader(){
        this.head.setVisibility(View.VISIBLE);
        return this;
    }

    /**
     * 隐藏提交按钮
     */
    public BaseDialog hideSubmit(){
        this.submit.setVisibility(View.GONE);
        return this;
    }

    /**
     * 隐藏取消按钮
     */
    public BaseDialog hideCancl(){
        this.cancel.setVisibility(View.GONE);
        return this;
    }

    public void hidden(){
        box.setVisibility(View.GONE);
    }

    public void visibility(){
        box.setVisibility(View.VISIBLE);
    }


    /**
     * 显示
     * @return
     */
    public void show(){
          show(0, Gravity.BOTTOM);
    }

    /**
     * 显示
     */
    public void show(int position){
        show(position, Gravity.BOTTOM);
    }
    public void show(int position,int gravity){
        this.isShow = true;
        if(gravity== Gravity.BOTTOM){
            setBg(R.drawable.basedialog_sharp);
        }else{
            setBg(R.drawable.basedialog_sharp2);
            setPaddingLR(DisplayUtil.dip2px(context,16));
        }
        this.position = position;
        getWindow().setGravity(gravity);
        super.show();
    }

    public void dismiss(){
        onClose();
        super.dismiss();
    }

    public abstract void onClose();
    public abstract boolean onCancel();
    public abstract boolean onSubmit();
    private OnResult onResult;

    public OnResult getOnResult() {
        return onResult;
    }

    public void setOnResult(OnResult onResult) {
        this.onResult = onResult;
    }

    public interface OnResult{
        void onYes();
        void onNo();
    }
}
